<?php
namespace app\common\model;
use app\admin\model\Admin;
use think\Model;
use think\model\concern\SoftDelete;

class CustomerHetong extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;

    protected $type = [
        'start_time'   => 'timestamp:Y-m-d',
        'end_time'     => 'timestamp:Y-m-d',
        'create_time'     => 'timestamp:Y-m-d',

    ];

    /**
     * 关联用户
     * @return \think\model\relation\BelongsTo
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class,'customer_id','id')->bind(['customer_name'=>'name']);
    }

    /**
     * 关联用户
     * @return \think\model\relation\BelongsTo
     */
    public function type()
    {
        return $this->belongsTo(DictData::class,'type_id','id')->bind(['type_name'=>'name']);
    }

    /**
     * 关联用户
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(Admin::class,'user_id','id')->bind(['user_name'=>'name']);
    }

    /**
     * 到期天数
     * @param $value
     * @param $data
     * @return int
     */
    public function getExpDayAttr($value,$data)
    {
        //过期日期
        $exp_time = $data['end_time'];
        //当前时间
        $times = $exp_time-time();
        //计算时间戳
        $day = $times/86400;
        return intval($day);
    }


}